<?php

class Sistema_CordetalheController extends App_Controller_Action
{
    private $_table     = 'CORDETALHA';
    private $_pk        = 'CRD_ID';
    private $_order_col = 'CRD_ID';
    private $_order     = 'ASC';
    private $_sta_col   = 'CRD_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'CRD_FILE';
    public  $dir = 'public/upload/cordetalhe/';
    //public  $db = '';
     
    public function init()
    {   
        //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $this->obj = new Sistema_Model_DbTable_Cordetalhe();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Cordetalhe();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        $gcd_id = $this->_getParam('gcd_id', 0);

        $this->view->gcd_id = $gcd_id;
        
        if($gcd_id == 0){
            $urlOptions = array('controller'=>'gradecordetalhe','action'=>'index');
            $this->_helper->redirector->gotoRoute($urlOptions);
        }
        
        $where = 'GCD_ID = '.$gcd_id;
               
        $result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $this->view->result = $this->paginacao($result);
        
        $this->view->voltar =  $this->_urlbase . '/' . $this->_modulo . '/gradecordetalhe';
    }
    
    public function editAction() {
       
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_geral.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/colorpicker/css/colorpicker.css'); 
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/colorpicker/css/layout.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/colorpicker.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/eye.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/utils.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/layout.js?ver=1.0.2');
        
        $gcd_id = $this->_getParam('gcd_id', 0);

         if($gcd_id == 0){
            $urlOptions = array('controller'=>'gradecordetalhe','action'=>'index');
            $this->_helper->redirector->gotoRoute($urlOptions);
        }
        
        $this->form->GCD_ID->setValue($gcd_id);
        
        Zend_Loader::loadClass('imagem','library');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk,0);
                    
                    $returnImg = imagem::upload($this->_file_col,$this->dir,$id);
                    
                    $data = $this->form->getValues();
                    unset($data['img2']);
                    unset($data[$this->_file_col]);
                    
                    if($id == 0){
                        $this->obj->insert($data);
                        $id =  (int) $this->obj->getAdapter()->lastInsertId();             
                        imagem::rename($id,$this->dir,$returnImg['filenew']);
                    }
                    else $this->obj->update($data, $this->_pk.' = ' . $id);  
                    
                    
                    if(strlen($_FILES[$this->_file_col]['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        if(strlen($filetemp) > 1) imagem::reizeFull($filename,$filename, '15', '30');
                    }
                    
                    $urlOptions = array('controller'=>$this->_controller,'action'=>'index','gcd_id'=>$gcd_id);
                    $this->_helper->redirector->gotoRoute($urlOptions);
                    //$this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData); 
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                echo $ex->getMessage();
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        $gcd_id = $this->_getParam('gcd_id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);  
            $ok = (bool) $this->obj->delete($this->_pk . ' = ' . $id); 
            
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir);
        }       
        //echo json_encode(array('ok' => $ok));
        $urlOptions = array('controller'=>$this->_controller,'action'=>'index','gcd_id'=>$gcd_id);
        $this->_helper->redirector->gotoRoute($urlOptions);
    }

}
